//
//  MeCommonCell.m
//  99SVR
//
//  Created by jiangys on 16/6/2.
//  Copyright © 2016年 xia zhonglin . All rights reserved.
//

#import "MeCommonCell.h"
#import "CommonCellLabelItem.h"
#import "CommonCellArrowItem.h"

@interface MeCommonCell()
/** 箭头 */
@property (nonatomic, strong) UIImageView *rightArrow;
/** 标签 */
@property (nonatomic, strong) UILabel *rightLabel;
/** 图标 这里不使用自带的self.imageView.image，因为底部横线不会包括自带的图标*/
@property(nonatomic, strong) UIImageView *iconImage;
@end

@implementation MeCommonCell

#pragma mark - 懒加载右边的view
- (UIImageView *)rightArrow
{
    if (_rightArrow == nil) {
        UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_point_icon"]];
        arrowImageView.size = (CGSize){13,13};
        self.rightArrow = arrowImageView;
    }
    return _rightArrow;
}

- (UILabel *)rightLabel
{
    if (_rightLabel == nil) {
        self.rightLabel = [[UILabel alloc] init];
        self.rightLabel.textColor = COLOR_Text_Gay;
        self.rightLabel.font = Font_14;
    }
    return _rightLabel;
}

#pragma mark - 初始化
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"MeCommonCell";
    MeCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[MeCommonCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _iconImage = [[UIImageView alloc] init];
        _iconImage.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_iconImage];
        
        // 设置标题的字体
        self.textLabel.font = [UIFont boldSystemFontOfSize:15.0f];
        self.textLabel.textColor = COLOR_Text_Black;
        self.detailTextLabel.font = Font_14;
        
        // 设置cell的默认背景色
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark - 调整子控件的位置
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 调整子标题的x
    self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) + 5;
    
    if (_item.icon) {
        self.textLabel.x = 50;
        float width = 20.0;
        _iconImage.frame = CGRectMake(15,(self.height-width)/2.0, width, width);
    }
    
    // 由于箭头UI尺寸不符合，需要单独设置偏右移
    self.rightArrow.x = self.width - 23;
}

- (void)setItem:(CommonCellItem *)item
{
    _item = item;
    
    // 1.设置基本数据
    if (item.icon) {
        _iconImage.image = [UIImage imageNamed:item.icon];
    } else {
        _iconImage.frame = CGRectZero;
    }
    //self.imageView.image = [UIImage imageNamed:item.icon];
    self.textLabel.text = item.title;
    self.detailTextLabel.text = item.subtitle;
    
    // 2.设置右边的内容
    if ([item isKindOfClass:[CommonCellArrowItem class]]) {
        self.accessoryView = self.rightArrow;
    } else if ([item isKindOfClass:[CommonCellLabelItem class]]) {
        CommonCellLabelItem *labelItem = (CommonCellLabelItem *)item;
        // 设置文字
        self.rightLabel.text = labelItem.text;
        // 根据文字计算尺寸
        self.rightLabel.size = [labelItem.text sizeMakeWithFont:self.rightLabel.font];
        self.accessoryView = self.rightLabel;
    } else { // 取消右边的内容
        self.accessoryView = nil;
    }
}
@end
